package com.jwj.testback.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class TakeTimeUtil {

    public static String dateDiff(Date startDate, Date endDate) throws Exception {
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = sd.format(startDate);
        String endTime = sd.format(endDate);

        long nd = 1000*24*60*60;//一天的毫秒数
        long nh = 1000*60*60;//一小时的毫秒数
        long nm = 1000*60;//一分钟的毫秒数
        long ns = 1000;//一秒钟的毫秒数
        long diff;
        //计算两个时间的毫秒时间差异
        diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
        long day = diff/nd;
        long hour = diff%nd/nh;
        long min = diff%nd%nh/nm;
        long sec = diff%nd%nh%nm/ns;
//        System.out.println(startTime+" ~ "+endTime+"时间间隔：\n"+day+"天"+hour+"小时"+min+"分钟"+sec+"秒");
        String result = day+"天"+hour+"时"+min+"分"+sec+"秒";
//        System.out.println(startTime+" ~ "+endTime+"时间间隔：\n"+day+"："+hour+"："+min+"："+sec);
        return result;
    }
}
